﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Categorys
{
    internal class MenuDAO
    {
        #region general

        private static Menu LoadObj(IDataReader datareader)
        {
            
            Menu obj = new Menu();
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.MenuCode = MpDataHelp.GetString(datareader, "MenuCode");
            obj.MenuTitle = MpDataHelp.GetString(datareader, "MenuTitle");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");
            return obj;
        }

        #endregion

        public static int Menu_Add(Menu obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Menu_Add");
            db.AddInParameter(dbCommand, "MenuCode", DbType.String, obj.MenuCode);
            db.AddInParameter(dbCommand, "MenuTitle", DbType.String, obj.MenuTitle);
            db.AddInParameter(dbCommand, "SortOrder", DbType.String, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }
     
        public static int Menu_Update(Menu obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Menu_Update");
            db.AddInParameter(dbCommand, "MenuCode", DbType.String, obj.MenuCode);
            db.AddInParameter(dbCommand, "MenuTitle", DbType.String, obj.MenuTitle);
            db.AddInParameter(dbCommand, "SortOrder", DbType.String, obj.SortOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Menu_Delete(string MenuCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Menu_Delete");
            db.AddInParameter(dbCommand, "MenuCode", DbType.String, MenuCode);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Menu> Menu_List()
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Menu_List");
            List<Menu> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Menu>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
